package com.game.centerserver.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.game.centerserver.common.Result;
import com.game.centerserver.infrastructure.entity.Order;
import com.game.centerserver.infrastructure.entity.Product;
import com.game.centerserver.infrastructure.entity.User;
import com.game.centerserver.infrastructure.service.ProductService;
import com.game.centerserver.infrastructure.service.UserService;
import com.game.centerserver.pojo.request.OrderRequest;
import com.game.centerserver.service.OrderServiceService;
import com.game.centerserver.service.UserServiceService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: tian
 * @Date: 2024/11/19/16:05
 * @Description:
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private OrderServiceService orderServiceService;
    @Resource
    private ProductService productService;
    @Resource
    private UserService userService;
    @Resource
    private UserServiceService userServiceService;

    @GetMapping("/queryOrderByOrderNum")
    public Result queryOrderByOrderNum(String orderNum){
        Order order = orderServiceService.queryById(orderNum);
        return Result.ok("查询成功",order);
    }

    @PostMapping("/addOrder")
    public Result<Order> addOrder(Order order){
        order.setStartTime(new Date());
        order.setCreateTime(new Date());
        if (order.getDuration() == null ||
                order.getUid() == null ||
                order.getProductId() == null ||
                order.getBillAmount() == null
        ) {
            return Result.fail("添加订单失败, 表单信息不全");
        }

        if (order.getDuration() <= 0) {return Result.fail("添加订单失败, 不允许添加负值或零时长");}


        Product product = productService.queryById(order.getProductId());
        if (product == null) {
            return Result.fail("添加订单失败, 产品ID不存在");
        }

        if (!userServiceService.isAdmin()) {
            String currID = StpUtil.getLoginIdAsString();
            User user = userService.queryById(currID);
            if (user == null) {
                return Result.fail("添加订单失败, 用户不存在");
            }
            if (!user.getUid().equals(order.getUid())) {
                return Result.fail("添加订单失败, 用户错误");
            }
        }

        if (order.getBillAmount().floatValue() < 0.0f) {
            return Result.fail("添加订单失败, 金额错误");
        }

        Order insert = orderServiceService.insert(order);
        if (insert != null){
            return Result.ok("添加订单成功", insert);
        }else {
            return Result.fail("添加订单失败");
        }
    }

    @DeleteMapping("/deleteOrderByOrderNum")
    public Result deleteOrder(String orderNum){
        boolean delete = orderServiceService.deleteById(orderNum);
        if (delete){
            return Result.ok("删除订单成功");
        }else {
            return Result.fail("删除订单失败");
        }
    }

    @PutMapping("/updateOrder")
    public Result updateOrder(Order order){
        boolean update = orderServiceService.update(order);
        if (update){
            return Result.ok("修改订单成功");
        }else {
            return Result.fail("修改订单失败");
        }
    }

    /**
     * 分页
     * @param orderRequest
     * @return
     */
    @GetMapping("/page")
    public Result page(OrderRequest orderRequest) {
        Page<Order> page = null;
        try {
            page = orderServiceService.page(orderRequest);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return Result.ok("订单分页成功", page);
    }


}
